package com.polito.mad.bikeponyapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.polito.mad.bikeponyapp.R;

public class HelpActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setDisplayShowTitleEnabled(true);
		getActionBar().setTitle(getString(R.string.help_page_activity_title));
		setTitle(getString(R.string.help_page_activity_title));
		
		setContentView(R.layout.activity_help);
		
		final ListView listview = (ListView) findViewById(R.id.help_page_list_view);
		
	    List<HelpList> myHelpList = new ArrayList<HelpActivity.HelpList>();
	    
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_introduction_heading), 
	    				getResources().getString(R.string.help_page_introduction),
	    				R.drawable.logo)
	    		);
	    myHelpList.add(	    					    
	    		new HelpList(
	    				getResources().getString(R.string.help_page_login_heading), 
	    				getResources().getString(R.string.help_page_login),
	    				R.drawable.login_icon)
	    		);
	    myHelpList.add(	    					    
	    		new HelpList(
	    				getResources().getString(R.string.help_page_logoff_heading), 
	    				getResources().getString(R.string.help_page_logoff),
	    				R.drawable.pending_auth_icon)
	    		);
	    myHelpList.add(	    					    
	    		new HelpList(
	    				getResources().getString(R.string.help_page_editprofile_heading), 
	    				getResources().getString(R.string.help_page_editprofile),
	    				R.drawable.edit_profile_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_main_view_heading), 
	    				getResources().getString(R.string.help_page_main_view),
	    				R.drawable.schedule_list)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_map_view_heading), 
	    				getResources().getString(R.string.help_page_map_view),
	    				R.drawable.location_map)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_pickup_point_reached_heading), 
	    				getResources().getString(R.string.help_page_pickup_point_reached),
	    				R.drawable.pickup_point_notification_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_track_code_verficiation_heading), 
	    				getResources().getString(R.string.help_page_track_code_verficiation),
	    				R.drawable.track_code_verification_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_delivery_point_reached_heading), 
	    				getResources().getString(R.string.help_page_delivery_point_reached),
	    				R.drawable.delivery_point_notification_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_delivery_code_verificaiton_heading), 
	    				getResources().getString(R.string.help_page_delivery_code_verificaiton),
	    				R.drawable.delivery_code_verification_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_signature_heading), 
	    				getResources().getString(R.string.help_page_signature),
	    				R.drawable.signature_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_feedback_heading), 
	    				getResources().getString(R.string.help_page_feedback),
	    				R.drawable.feedback_icon)
	    		);
	    myHelpList.add(
	    		new HelpList(
	    				getResources().getString(R.string.help_page_pending_task_alert_heading), 
	    				getResources().getString(R.string.help_page_pending_task_alert),
	    				R.drawable.new_notification_animation_draw)
	    		);
	    
	    final HelpListArrayAdapter adapter = new HelpListArrayAdapter(this, myHelpList);
	    listview.setAdapter(adapter);

	}
	
	//container class to have each item definition of help manual
	private class HelpList{
		String title;
		String content;
		int imgID = R.drawable.ic_launcher;
		
		public int getImgPath() {
			return imgID;
		}
		public HelpList(String title, String content, int imageId)
		{
			this.title = title;
			this.content = content;
			this.imgID = imageId;
			
		}
		public String getTitle() {
			return title;
		}
		public String getContent() {
			return content;
		}
		
	}
	
	private class HelpListArrayAdapter extends BaseAdapter {

		private Context context;
	    
	    List<HelpList> stringList = new ArrayList<HelpList>();
	    
	    public HelpListArrayAdapter(Context context, List<HelpList> objects) {
	    	this.context = context;
	    	stringList.addAll(objects);
	    }
	    
	    @Override
	    public long getItemId(int position) {
	    	return position;
	    }
	    
	    @Override
	    public boolean hasStableIds() {
	      return true;
	    }
	    
	    @Override
	    public View getView(int position, View convertView, ViewGroup parent) {
	    	
	    	if(convertView == null)
			{
				LayoutInflater inflater = (LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.help_manual_layout, parent, false);
			}
	    	
	    	TextView headingTV = (TextView)convertView.findViewById(R.id.help_manual_item_heading);
	    	headingTV.setText(stringList.get(position).getTitle());
	    	
	    	TextView detailsTV = (TextView)convertView.findViewById(R.id.help_manual_item_content);
	    	detailsTV.setText(stringList.get(position).getContent());
	    	
	    	ImageView imgView = (ImageView)convertView.findViewById(R.id.help_manual_image);
	    	imgView.setImageResource(stringList.get(position).getImgPath());
	        
	    	return convertView;//super.getView(position, convertView, parent);
	    }

		@Override
		public int getCount() {
			return stringList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return stringList.get(arg0);
		}

	  }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.help, menu);
		return false;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home)
		{
			Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
		}else
			return super.onOptionsItemSelected(item);
	}

}
